package com.tkjt.pms.modules.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tkjt.pms.common.entity.TkUserReportDepart;
import com.tkjt.pms.common.utils.EsDateUtils;
import com.tkjt.pms.common.utils.OrgGroupUtil;
import com.tkjt.pms.modules.common.mapper.PMSCommonMapper;
import com.tkjt.pms.modules.common.mapper.TkUserReportDepartMapper;
import com.tkjt.pms.modules.common.service.PMSCommonService;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.system.entity.SysConfig;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.mapper.SysConfigMapper;
import org.jeecg.modules.system.mapper.SysDepartMapper;
import org.jeecg.modules.system.model.DepartIdModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("commonService")
public class PMSCommonServiceImpl implements PMSCommonService {
    @Autowired
    private PMSCommonMapper commonMapper;
    @Autowired
    private SysDepartMapper sysDepartMapper;
    @Autowired
    private SysConfigMapper sysConfigMapper;
    @Autowired
    private TkUserReportDepartMapper tkUserReportDepartMapper;
    @Override
    public List<Map<String, Object>> yearMonthList() {
        List<Map<String, Object>> list = new ArrayList<>();
        String currentYearMonth = EsDateUtils.getIndexYearMonth(new Date());
        String month = EsDateUtils.getIndexMonth(currentYearMonth);
        String year = EsDateUtils.getIndexYear(currentYearMonth);
        int monthInt = 0;
        if(month.startsWith("0")){
            monthInt = Integer.valueOf(month.substring(1));
        }else{
            monthInt = Integer.valueOf(month);
        }
        for(int i = monthInt ; i > 0 ; i--){
            Map<String,Object> map = new HashMap<>();
            if(i < 10){
                month = "0" + i;
            }else{
                month = String.valueOf(i);
            }
            map.put("value",year+month);
            map.put("text",year+"年"+month+"月");
            list.add(map);
        }

        return list;
    }

    @Override
    public List<Map<String, Object>> allMTotalYearMonthList() {
        List<Map<String, Object>> list = new ArrayList<>();
        String currentYearMonth = EsDateUtils.getIndexYearMonth(new Date());
        String month = EsDateUtils.getIndexMonth(currentYearMonth);
        String year = EsDateUtils.getIndexYear(currentYearMonth);
        int monthInt = 0;
        if(month.startsWith("0")){
            monthInt = Integer.valueOf(month.substring(1));
        }else{
            monthInt = Integer.valueOf(month);
        }
        for(int i = monthInt ; i > 1 ; i--){
            Map<String,Object> map = new HashMap<>();
            if(i < 10){
                month = "0" + i;
            }else{
                month = String.valueOf(i);
            }
            map.put("value",year+"01"+month);
            map.put("text",year+"年1-"+i+"月累计");
            list.add(map);
        }

        return list;
    }

    @Override
    public List<Map<String, Object>> currentMTotalYearMonthList() {
        List<Map<String, Object>> list = new ArrayList<>();
        String currentYearMonth = EsDateUtils.getIndexYearMonth(new Date());
        String month = EsDateUtils.getIndexMonth(currentYearMonth);
        String year = EsDateUtils.getIndexYear(currentYearMonth);
        int monthInt = 0;
        if(month.startsWith("0")){
            monthInt = Integer.valueOf(month.substring(1));
        }else{
            monthInt = Integer.valueOf(month);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("value",year+"01"+month);
        map.put("text",year+"年1-"+monthInt+"月累计");
        list.add(map);

        return list;
    }

    @Override
    public List<Map<String, Object>> checkYearList() {
        List<Map<String, Object>> list = new ArrayList<>();
        String currentYearMonth = EsDateUtils.getIndexYearMonth(new Date());
        Integer year = Integer.valueOf(EsDateUtils.getIndexYear(currentYearMonth));
        for(int i = year ; i >= 2020 ; i--){
            Map<String,Object> map = new HashMap<>();
            map.put("value",i);
            map.put("text",i+"年");
            list.add(map);
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> getOrgCodeCG() {
        List<Map<String, Object>> list = commonMapper.getOrgCodeCG();
        List<Map<String, Object>> dicts = new ArrayList<>();
        for (Map<String,Object> orgMap : list){
        	//TEMP
        	if("事业群单位".equals(orgMap.get("text"))){
        		orgMap.put("text", "养康本部");
        	}
        	//这个函数必须修改
            if(orgMap.get("value").toString().length() == 9){
                Map<String,Object> parentMap = new HashMap<>();
                parentMap.putAll(orgMap);
                List<Map<String, Object>> childDicts = new ArrayList<>();
                for (Map<String,Object> item : list){
                    if(parentMap.get("value").toString().equals(item.get("value").toString().substring(0,9)) && !item.get("text").toString().contains("养康全国")){
                        childDicts.add(item);
                    }
                }
                parentMap.put("orgs",childDicts);
                dicts.add(parentMap);
            }
        }
        return dicts;
    }

    @Override
    public List<Map<String, Object>> gradeOrgs() {
        List<Map<String, Object>> list = commonMapper.getOrgCodeCG();
        List<Map<String, Object>> dicts = new ArrayList<>();
        for (Map<String,Object> orgMap : list){
            if(orgMap.get("value").toString().length() == 9){
                Map<String,Object> parentMap = new HashMap<>();
                parentMap.putAll(orgMap);
                List<Map<String, Object>> orgs = new ArrayList<>();
                List<Map<String, Object>> exeOrgs = new ArrayList<>();
                for (Map<String,Object> item : list){
                    if(parentMap.get("value").toString().equals(item.get("value").toString().substring(0,9))){
                        orgs.add(item);
                    }else{
                        exeOrgs.add(item);
                    }
                }
                parentMap.put("orgs",orgs);
                parentMap.put("exeOrgs",orgs);
                dicts.add(parentMap);
            }
        }
        return dicts;
    }

    @Override
    public List<Map<String, Object>> getAllOrgs() {
        return commonMapper.getAllOrgs();
    }

    @Override
    public List<Map<String, Object>> indexCodes(String indexGroup,String indexType,String indexEdit,List<String> indexAttrs,List<String> noIndexAttrs) {
        Map<String,Object> param = new HashMap<>();
        if(StringUtils.isNotEmpty(indexGroup)){
            String orgGroup = OrgGroupUtil.changeToCG(indexGroup);
            param.put("indexGroup",orgGroup);
        }
        param.put("indexType",indexType);
        param.put("indexAttrs",indexAttrs);
        param.put("noIndexAttrs",noIndexAttrs);
        param.put("indexEdit",indexEdit);
        List<Map<String, Object>> list = commonMapper.getIndexCodeByParam(param);
        return list;
    }

    @Override
    public List<Map<String, Object>> indexValueCodes(String indexGroup,String indexCode) {
        Map<String,Object> param = new HashMap<>();
        param.put("indexCode",indexCode);
        if(StringUtils.isNotEmpty(indexGroup)){
            String orgGroup = OrgGroupUtil.changeToCG(indexGroup);
            param.put("indexGroup",orgGroup);
        }
        List<Map<String, Object>> list = commonMapper.getIndexValueCodeByParam(param);
        return list;
    }

    @Override
    public List<Map<String, Object>> cgOrgs(String indexGroup) {
        QueryWrapper<SysDepart> qw = new QueryWrapper<>();
        qw.likeRight("org_code",indexGroup.equals("C")?"A01A01A01":"A01A01A02");
        qw.eq("org_type","2");
        List<SysDepart> sysDeparts = sysDepartMapper.selectList(qw);
        List<Map<String, Object>> list =new ArrayList<>();
        for (SysDepart s : sysDeparts){
            Map<String,Object> param = new HashMap<>();
            param.put("name",s.getDepartName());
            param.put("code",s.getOrgCode());
            list.add(param);
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> getIndexCodeByParam(Map<String, Object> map) {
        return commonMapper.getIndexCodeByParam(map);
    }

    @Override
    public SysConfig getSysConfig(String configCode) {
        QueryWrapper<SysConfig> sysConfigWrapper = new QueryWrapper<>();
        sysConfigWrapper.lambda().eq(SysConfig::getConfigCode,configCode);
        SysConfig sysConfig = sysConfigMapper.selectOne(sysConfigWrapper);
        return sysConfig;
    }

    /**
     * 根据用户id查询报表部门信息
     */
    @Override
    public List<DepartIdModel> userReportDepartList(String userId) {
        QueryWrapper<TkUserReportDepart> queryUDep = new QueryWrapper<TkUserReportDepart>();
        QueryWrapper<SysDepart> queryDep = new QueryWrapper<SysDepart>();
        try {
            queryUDep.lambda().eq(TkUserReportDepart::getUserId, userId);
            List<String> depIdList = new ArrayList<>();
            List<DepartIdModel> depIdModelList = new ArrayList<>();
            List<TkUserReportDepart> userDepList = tkUserReportDepartMapper.selectList(queryUDep);
            if(userDepList != null && userDepList.size() > 0) {
                for(TkUserReportDepart userDepart : userDepList) {
                    depIdList.add(userDepart.getDepId());
                }
                queryDep.lambda().in(SysDepart::getId, depIdList);
                List<SysDepart> depList = sysDepartMapper.selectList(queryDep);
                if(depList != null || depList.size() > 0) {
                    for(SysDepart depart : depList) {
                        depIdModelList.add(new DepartIdModel().convertByUserDepart(depart));
                    }
                }
                return depIdModelList;
            }
        }catch(Exception e) {
            e.fillInStackTrace();
        }
        return null;


    }
}
